package mysql

import (
	"gorm.io/gorm"
	"house_srv/housesrv"
)

type House struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(20);comment:物业名称" json:"name"`
	Address string  `gorm:"type:varchar(100);comment:地址" json:"address"`
	Types   string  `gorm:"type:varchar(20);comment:房型" json:"types"`
	Size    float64 `gorm:"type:decimal(10,2);comment:面积" json:"size"`
	Price   float64 `gorm:"type:decimal(10,2);comment:租金" json:"price"`
}

// AddHouse 房源发布
func (h *House) AddHouse() error {
	return DB.Debug().Create(&h).Error
}

// InfoHouse 房源详情
func (h *House) InfoHouse(id int) error {
	return DB.Debug().Where("id = ?", id).Limit(1).Find(&h).Error
}

// ListHouse 房源列表
func (h *House) ListHouse() (lists []*housesrv.HouseList, err error) {
	var houses []House
	DB.Debug().Find(&houses)
	for _, v := range houses {
		list := housesrv.HouseList{
			Id:      int64(v.ID),
			Name:    v.Name,
			Address: v.Address,
			Types:   v.Types,
			Size:    float32(v.Size),
			Price:   float32(v.Price),
		}
		lists = append(lists, &list)
	}
	return lists, err
}
